<?php
namespace app\index\controller;
use app\index\model\Admin as Admins;
use app\index\model\Authgroup as Authgroups;
use app\index\model\Authgroupaccess as Authgroupaccesss;
use app\index\model\Authrule as Authrules;
use app\index\model\Agency as Agencys;
use app\index\model\User as Users;
use app\index\model\Shopregion;
use app\index\model\Useradmin;
use think\Session;
use yii\debug\models\timeline\Search;

class Admin extends Comm{
    //管理员->权限组管理显示启用和禁止
    public function admingroup(){
        $admins=new Admins();
        $res=$admins->adminGroupAll();
        $table="管理员-权限组管理";
        $tab=["业务员","权限组个数",'操作'];
        return view('admingroup',['res'=>$res,'table'=>$table,'tab'=>$tab]);
    }
    //管理员添加权限组
    public function adminadd(){
        //查询所有管理员
        $admins=new Admins();
        $user=$admins->secondadmin();
        //查询所有权限组id
        $ids=[];
        if(isset($_GET['id'])){
            $ids=$admins->ids($_GET['id']);
        }
        //查询所有权限组
        $authgroups=new Authgroups();
        $groupname=$authgroups->AllData();
        return view('adminadd',['user'=>$user,'group'=>$groupname,'ids'=>$ids]);
    }
    //删除管理员-权限组
    public function deleteadmingroup(){
        $id=$_GET['id'];
        $Authgroupaccesss=new Authgroupaccesss();
        $res=$Authgroupaccesss->del($id);
        $this->success("清空成功！",'admingroup');
    }
    //给管理员修改权限组
    public function adminaddfrom(){
        $data['groupid']=implode(',',$_POST['groupid']);
        $data['adminid']=$_POST['adminid'];
        $authgroupaccesss=new Authgroupaccesss();
        $user=$authgroupaccesss->addres($data);
        $this->success("修改成功！",'admingroup');
    }
    //权限管理显示/开启和禁用具体规则
    public function rules(){
        $authrules=new Authrules();
        $res=$authrules->AllData();
        $table="权限管理";
        $name=["title","status","id"];
        $tab=["权限名","状态",'操作'];
        return view('rules',['res'=>$res,'table'=>$table,'name'=>$name,'tab'=>$tab,'url'=>'rulesstatus']);
    }
    //切换具体权限的禁用和启用
    public function rulesstatus(){
        $id=$_GET['id'];
        $authrules=new Authrules();
        $res=$authrules->updateStatus($id);
        $this->redirect('rules');
    }
    //权限组管理显示启用和禁止
    public function groups(){
        $authgroups=new Authgroups();
        $res=$authgroups->AllData();
        $table="权限组管理";
        $name=["title","status","id"];
        $tab=["权限组名","状态",'操作'];
        return view('groups',['res'=>$res,'table'=>$table,'name'=>$name,'tab'=>$tab]);
    }
    //切换权限组的启用和禁用
    public function groupstatus(){
        $id=$_GET['id'];
        $authgroups=new Authgroups();
        $res=$authgroups->updateStatus($id);
        $this->redirect('groups');
    }
    //修改权限组
    public function updategroup(){
        $id=$_GET['id'];
        $authgroups=new Authgroups();
        $ids=$authgroups->onerules($id);
        $one=$authgroups->one($id);
        $authrules=new Authrules();
        $rules=$authrules->AllData();
        return view('groupupdate',['rules'=>$rules,'ids'=>$ids,'title'=>$one['title']]);
    }
    //添加权限组
    public function addgroup(){
        $authrules=new Authrules();
        $rules=$authrules->AllData();
        return view('groupadd',['rules'=>$rules]);
    }
    //修改权限组的具体规则
    public function updategrouprule(){
        $id=$_POST['id'];
        $rules=implode(',',$_POST['rulesid']);
        $title=$_POST['title'];
        $authgroups=new Authgroups();
        $res=$authgroups->updategroup($id,$rules,$title);
        $this->success("修改成功！",'groups');
    }
    //添加权限组规则
    public function groupsaddfrom(){
        $title=$_POST['title'];
        $rules=implode(',',$_POST['rulesid']);
        $authgroups=new Authgroups();
        $res=$authgroups->addgroup($title,$rules);
        $this->success("添加成功！",'groups');
    }
    //删除权限组
    public function groupsdelete(){
        $id=$_GET['id'];
        $authgroups=new Authgroups();
        $res=$authgroups->del($id);
        $this->success("删除成功！",'groups');
    }
    //显示管理员列表
    public function index()
    {
        $Admins = new Admins();
        $data = $Admins->select();
        return view('index',['data'=>$data]);
    }
    //添加管理员
    public function add()
    {
        if(request()->isPost())
        {//返回结果->如果是Post
            //dump(input('post.'));//以数组形式打印post提交的数据
            $result = $this->validate(
                [
                    '账号' => input('account'),
                    '密码' => input('root'),

                ],
                [
                    '账号' => 'require|max:15|min:4',
                    '密码' => 'require|max:15|min:6',
                ]);
            if(true !== $result){
                return $this->error($result);
            }else{
                if(input('root')==input('roots'))
                {
                    $Admins = new Admins();
                    $randomcode = $Admins->randomcode();
                    $Admins->data([
                        'isadmin' => input('Administrator'),
                        'account' => input('account'),
                        //把add.html中id为account的input标签里用户输入的内容，保存到account这个变量里
                        'root' => md5(input('root')).$randomcode,
                        'randomcode' =>$randomcode
                        //把add.html中id为password的input标签里用户输入的内容，保存到password这个变量里
                    ]);
                    if($Admins->save()==true)
                    {   
                        zsgc('添加了管理员['.input('account').']');
                        return $this->success('添加管理员成功!','index');
                    }else
                    {
                        return $this->error('添加管理员失败!');
                    }
                }else
                {
                    return $this->error('两次密码不一致!');
                }
            }
        }else
        {
            return view('add');
        }

    }
    //删除管理员
    public function del()
    {
        $id=input('id');
        $result = Admins::get($id);
        if($result['isadmin']=="超级管理员"){
            $this->error('权限相同无法删除！','index');
            die();
        }
        $account = $result['account'];
        $result = Admins::destroy($id);
        if($result)
        {   
            zsgc('删除了管理员['.$account.']');
            $this->success('删除成功','index');
        }else
        {
            $this->error('删除失败','index');
        }
    }
    public function edit()
    {
        $id=input('id');
        $result = Admins::get($id);
        if($result['isadmin']=="超级管理员"){
            $this->error('权限相同无法修改！','index');
            die();
        }
        if(isset($_POST['submit'])){
        $id = input('hid');
        $account = input('account');
        $isadmin = input('Administrator');
        $Admins = new Admins();
        $moddo = $Admins->edit($id,$account,$isadmin);
        if($moddo){
            zsgc('修改了管理员['.$account.']的信息');
            $this->success('修改成功','index');
        }else{
            $this->error('修改失败','index');
        }
        }
        //获取修改信息
        $id = input('id');
        $Admins = new Admins();
        $result = Admins::where('id',$id)->find();
        $this->assign('result',$result);//模板赋值
        return view('edit');
    }
    public function editmyself(){
        if(isset($_POST['submit'])){
            $id = input('hid');
            $account = input('account');
            $root2 = md5(input('root'));
            $Admins = new Admins();
            $querys = $Admins->where('id', $id)->find();
            $root1 = $querys['root'];
            $randomcode = $querys['randomcode'];

            if($root2.$randomcode == $root1){
               
                $moddo = $Admins->editmyself($id,$account);
            }else{
                $this->error('密码错误','index/admin/editmyself');
            }

            if($moddo){
                $this->success('修改成功,重新登陆后生效','index');
            }else{
                $this->error('修改失败','index');
            }
        }else{
             return view('editmyself');
        } 
    }
    public function editroot()
    {
        if(request()->isPost())
        {
            $Admin = new Admins();
            $se = session::get('id');
            $querys = $Admin->where('id', $se)->find();
            $root = $querys['root'];
            $randomcode = $querys['randomcode'];
            $newroot = md5(input('roots'));
            $rr = md5(input('root'));
            $r = $rr.$randomcode;
            if($r !== $root)
            {
                return $this->error('原密码输入错误');
            }else
            {
                $result = $this->validate(
                [
                    '新密码' => input('roots'),
                ],
                [
                    '新密码' => 'require|max:15|min:6',
                ]);
                if(true !== $result)
                {
                    return $this->error($result);
                }else
                {
                    if(input('roots') !==input('rootss'))
                        {
                            return $this->error('新密码与确认密码不一致');
                        }
                        else
                        {
                            $queryu = $Admin->editroot($se,$newroot);
                            if($queryu)
                            {
                                return $this->success('修改成功,请重新登录','index/Quit/Quit');
                            }else
                            {
                                return $this->error('修改失败');
                            }
                        }
                }   
            }
        }
        return view('editroot');
    }
    //业务员店铺管理
    public function business(){
        $admins=new Admins();
        $res=$admins->businessAll();
        return view('business',['res'=>$res]);
    }
    //修改业务员管理的店铺显示页面
    public function businessupdate(){
        $admins=new Admins();
        $res=$admins->OneData($_GET['id']);
        $shopregion=new Shopregion();
        $arr=$shopregion->SuperData($_GET['id']);
        $agencys=new Agencys();
        $ids=$agencys->AdminShopId(['adminid'=>$_GET['id']]);
        //echo '<pre>';print_r($ids);die;
        return view('businessupdate',['res'=>$res,'arr'=>$arr,'ids'=>$ids]);
    }
    //修改业务员管理的店铺
    public function businessupdatedata(){
        $id=$_POST['id'];
        $adminid=$_POST['adminid'];
        $agency=new Agencys();
        $res1=$agency->save(['adminid'=>$adminid],['id'=>['in',$id]]);
        $res2=$agency->save(['adminid'=>0],['adminid'=>['=',$adminid],'id'=>['not in',$id]]);
        if($res1||$res2)
            $this->success('操作成功！','admin/business');
        else
            $this->error('操作失败！','admin/business');
    }
    //清空业务员管理的店铺
    public function businessdelete(){
        $id=$_GET['id'];
        $agency=new Agencys();
        $res=$agency->Delbusiness($id);
        if($res)
            $this->success('清空该业务员管理的所有店铺成功！','admin/business');
        else
            $this->error('清空该业务员管理的所有店铺失败！','admin/business');
    }
    //团长店铺管理
    public function colonel(){
        $agency=new Agencys();
        $res=$agency->colonelAll(Session('id'),Session('isadmin'));
        return view('colonel',["res"=>$res]);
    }
    //团长管理的店铺修改
    public function colonelupdate(){
       $admins=new Admins();
       $res=$admins->OneData($_GET['adminid']);
       $shopregion=new Shopregion();
       $arr=$shopregion->SuperData($_GET['adminid'],$_GET['id']);
       $agencys=new Agencys();
       $ids=$agencys->AdminShopId(['manageid'=>$_GET['id']]);
       //echo '<pre>';print_r($ids);die;
        return view('colonelupdate',['res'=>$res,'arr'=>$arr,'ids'=>$ids]);
    }
    public function colonelupdatedata(){
        $ids=$_POST['id'];
        $manageid=$_POST['manageid'];
        $agency=new Agencys();
        $res=$agency->addcolonel($manageid,$ids);
        if($res)
            $this->success('操作成功！','admin/colonel');
        else
            $this->error('操作失败！','admin/colonel');
    }
    //清空团长管理的店铺
    public function coloneldelete(){
        $id=$_GET['id'];
        $agency=new Agencys();
        $res=$agency->Delcolonel($id);
        if($res)
            $this->success('清空该团长管理的所有店铺成功！','admin/colonel');
        else
            $this->error('清空该团长管理的所有店铺失败！','admin/colonel');
    }
    //修改团长的上级
    public function colonelupdateadmin(){
        $id=$_GET['id'];
        $users=new Users();
        $res=$users->oneData($id);
        $admins=new Admins();
        $admin=$admins->secondadmin();
        if(isset($_POST['sub'])){
            $useradmin=new Useradmin();
            $res=$useradmin->UpdateOneData($_POST['userid'],$_POST['adminid']);
            $agency=new Agencys();
            $res=$agency->Delcolonel($_POST['userid']);
            if($res)
                $this->success('操作成功！','admin/colonel');
            else
                $this->error('操作失败！','admin/colonel');
        }
        return view('colonelupdateadmin',['res'=>$res,'admin'=>$admin]);
    }
}